﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SAM_Edu.control;
using MySql.Data.MySqlClient;

namespace SAM_Edu.form
{
    public partial class frmManageAccessRights : Form
    {
        public frmManageAccessRights()
        {
            InitializeComponent();
        }
       
        private void BindControlValues()
        {
            DataSet dsUsername = ManageAccessRights.requestLoadUsername();

          
            for (int i = 0; i < dsUsername.Tables[0].Rows.Count; i++)
            {
                listBoxUsername.Items.Add(dsUsername.Tables[0].Rows[i][0].ToString());
               
            }
         
           
            
          
        }
        private void frmManageAccessRights_Load(object sender, EventArgs e)
        {
            BindControlValues();
            binddb();
           
        }
       

        DataSet dsFeatureList = ManageFeatures.requestFeatureList();
        private void binddb()
        {

          
            for (int i = 0; i < dsFeatureList.Tables[0].Rows.Count; i++)
            {
              listBoxFeatureList.Items.Add(dsFeatureList.Tables[0].Rows[i][0].ToString());
            
            }
          
        }


      
      //  ulong value = 0x0000000000000000;
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int resultofCheckBox = 0;
            ulong code = 0;
            for (int i = 0; i <= (listBoxFeatureList.Items.Count - 1); i++)
            {
                resultofCheckBox = listBoxFeatureList.GetItemChecked(i) ? 1 : 0 == CheckState.Checked ? 1 : 0;
                code = Convert.ToUInt64(dsFeatureList.Tables[0].Rows[i]["code"].ToString());

                gg += (ulong)resultofCheckBox * (ulong)code;
            }

            string username = listBoxUsername.SelectedItem.ToString();


            bool status = ManageFeatures.requestUpdateUserRights(username, gg);
            if (status == true)
            {
                MessageBox.Show("Rights given successfully!" + "value = " + gg);
            }
            else
            {
                MessageBox.Show("An error occured");
            }
        }

        private void tbsearch_TextChanged(object sender, EventArgs e)
        {
            listBoxUsername.Items.Clear();
            string search = tbsearch.Text;
            DataSet dsUsername = ManageAccessRights.requestLoadUsername(search);
            for (int i = 0; i < dsUsername.Tables[0].Rows.Count; i++)
            {
                listBoxUsername.Items.Add(dsUsername.Tables[0].Rows[i][0].ToString());
            }
           
         
        }

        private void getInformation()
        {
            string search = tbsearch.Text;
            DataSet dsUsername = ManageAccessRights.requestLoadUsername(search);
            string username = listBoxUsername.SelectedItem.ToString();
            for (int i = 0; i < dsUsername.Tables[0].Rows.Count; i++)
            {
                if (dsUsername.Tables[0].Rows[i][0].ToString() == username)
                {
                    tbusername.Text = dsUsername.Tables[0].Rows[i][0].ToString();
                    tbpw.Text = dsUsername.Tables[0].Rows[i][1].ToString();
                    tbsecQN.Text = dsUsername.Tables[0].Rows[i][2].ToString();
                    tbSecANS.Text = dsUsername.Tables[0].Rows[i][3].ToString();
                    tbrfidno.Text = dsUsername.Tables[0].Rows[i][4].ToString();
                    int uid = int.Parse(dsUsername.Tables[0].Rows[i][5].ToString());
                    DataSet dsgetUser = ManageAccessRights.requestLoadUID(uid);
                    tbfullname.Text = dsgetUser.Tables[0].Rows[0][1].ToString();
                    tbemail.Text = dsgetUser.Tables[0].Rows[0][2].ToString();
                    tbcountrycode.Text = dsgetUser.Tables[0].Rows[0][3].ToString();
                    tbareacode.Text = dsgetUser.Tables[0].Rows[0][4].ToString();
                    tbmobileno.Text = dsgetUser.Tables[0].Rows[0][5].ToString();
                    break;
                }
            }
            ulong getcode = ManageFeatures.requestUserRights(username);
            for (int i = 0; i < dsFeatureList.Tables[0].Rows.Count; i++)
            {
                ulong code = ulong.Parse(dsFeatureList.Tables[0].Rows[i][1].ToString());

                listBoxFeatureList.SetItemChecked(i, Convert.ToBoolean(((ulong)getcode & code) > 0));
            }
        }

        private void listBoxUsername_SelectedIndexChanged(object sender, EventArgs e)
        {
            getInformation();
           
          
              
            
        }
       static ulong gg = 0;
       
      
      
    }
}
